package backend.util;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class PasswordEncrypter {

	private static final Logger LOG = LoggerFactory
			.getLogger(PasswordEncrypter.class);

	public static String generateHashedPassword(final String password,
			final String salt) {
		String hashedPassword = "";
		byte[] initialBytes;
		try {
			initialBytes = (password + salt).getBytes(PropertyProvider.INSTANCE
					.getProperty("passwordEncoding"));
			final MessageDigest algorithm = MessageDigest
					.getInstance(PropertyProvider.INSTANCE
							.getProperty("passwordHashingAlgorithm"));
			algorithm.reset();
			algorithm.update(initialBytes);
			final byte[] hashedBytes = algorithm.digest();
			hashedPassword = new String(hashedBytes);
		} catch (final UnsupportedEncodingException e) {
			LOG.error("Password encryption failed: unsupported encoding", e);
		} catch (final NoSuchAlgorithmException nsae) {
			LOG.error(
					"Password encryption failed: hashing algorithm not supported",
					nsae);
		}
		return hashedPassword;
	}

}
